package cn.hrlly.tao2shou.view.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.RegisterPresenter;
import cn.hrlly.tao2shou.presenter.RegisterPresenterImpl;
import cn.hrlly.tao2shou.utils.GpsUtil;
import cn.hrlly.tao2shou.utils.ImageUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.CollectUserInfoView;

public class CollectUserInfoActivity extends BaseActivity implements CollectUserInfoView {

    @Bind(R.id.ivHeadProtrait)
    ImageView ivHeadProtrait;
    @Bind(R.id.etNickname)
    EditText etNickname;
    @Bind(R.id.spnGender)
    Spinner spnGender;
    @Bind(R.id.spnSchool)
    Spinner spnSchool;
    @Bind(R.id.btnConfirm)
    Button btnConfirm;
    private GpsUtil gpsUtil;
    private RegisterPresenter registerPresenter;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_user_info);
        //控件绑定
        ButterKnife.bind(this);
        //初始化定位工具
        gpsUtil=new GpsUtil(getApplicationContext());
        gpsUtil.startLocation();
        //初始化registerPresenter
        registerPresenter = new RegisterPresenterImpl(this, this);
        //获取registerActivity传来的值
        intent=getIntent();
    }

    //关闭数据库，停止定位
    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpsUtil.stopLocation();
        registerPresenter.closeDataBase();
    }

    //btnConfirm点击事件
    @OnClick(R.id.btnConfirm)
    public void confirm(){
        if(!etNickname.getText().toString().equals(""))
            registerPresenter.collectUserInfo();
        else
            showMessage(R.string.fill_incomplete);
    }

    //以下是对activity的一些操作
    @Override
    public String getNickname() {
        return etNickname.getText().toString();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public File getPortraitFile() {
        //将头像转换成file类型,开启一个线程进行操作
        Bitmap bitmap = ((BitmapDrawable) ivHeadProtrait.getDrawable()).getBitmap();
        bitmap = ImageUtil.compBitmapScale(bitmap, ivHeadProtrait.getMaxHeight(), ivHeadProtrait.getMaxWidth());
        File file = ImageUtil.bitmapToFile(bitmap, CollectUserInfoActivity.this);
        return file;
    }

    @Override
    public String getGender() {
        System.out.println(spnGender.toString());
        return spnGender.toString();
    }

    @Override
    public String getSchool() {
        return spnSchool.toString();
    }

    @Override
    public String getPhoneNumber() {
        return intent.getStringExtra(Config.key_phone_number);
    }

    @Override
    public String getPassword() {
        return intent.getStringExtra(Config.key_password);
    }

    @Override
    public double getLongitude() {
        return gpsUtil.returnLongitude();
    }

    @Override
    public double getLantitude() {
        return gpsUtil.returnLantitude();
    }

    @Override
    public void startActivity() {
        Intent intent=new Intent(CollectUserInfoActivity.this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }
}
